package com.example.pojos;

import java.io.Serializable;
import java.util.Comparator;

import android.os.Parcel;
import android.os.Parcelable;

public class Product implements Parcelable, Comparable<Product>, Serializable{
	private static final long serialVersionUID = 1384619936634792713L;
	private String name;
	private String shortDesc;
	private String longDesc;
	private String url;
	private float price;
	private Product_Type categorie;
	
	public static final Parcelable.Creator<Product> CREATOR =
			new Parcelable.Creator<Product>() {

				@Override
				public Product createFromParcel(Parcel source) {
					return new Product(source);
				}

				@Override
				public Product[] newArray(int size) {
					Product[] tab = new Product[size];
					for(int i=0; i<size; i++)
						tab[i] = new Product();
					return tab;
				}
				
			};
	
	public Product(){
	}
	
	public Product(Parcel in){
		name = in.readString();
		shortDesc = in.readString();
		longDesc = in.readString();
		url = in.readString();
		price = in.readFloat();
		categorie = Product_Type.valueOf(in.readString());
	}
	
	public Product(String name, String shortDesc,
			String longDesc, String url, float price, Product_Type type){
		this.name = name;
		this.shortDesc = shortDesc;
		this.longDesc = longDesc;
		this.url = url;
		this.price = price;
		this.categorie = type;
	}

	@Override
	public int compareTo(Product another) {
		if(this.price < another.price){
			return -1;
		}else if( this.price == another.getPrice()){
			return 0;
		}
		return 1;
	}
	
    public static Comparator<Product> COMPARE_BY_PRODUCTNAME = new Comparator<Product>() {
        public int compare(Product one, Product other) {
            return one.getName().compareTo(other.getName());
        }
    };
    public static Comparator<Product> COMPARE_BY_PRODUCTPRICE = new Comparator<Product>() {
        public int compare(Product one, Product other) {
            return one.compareTo(other);
        }
    };
	
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(name);
		dest.writeString(shortDesc);
		dest.writeString(longDesc);
		dest.writeString(url);
		dest.writeFloat(price);
		dest.writeString(categorie.name());
	}
	
	@Override
	public boolean equals(Object o) {
		if(o instanceof Product){
			Product other = (Product)o;
			if(price == other.getPrice() && name.equals(other.getName()))
				return true;
			else 
				return false;
		}
		else 
			return false;
	};
	
	
	
	// GETTERS & SETTERS
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getShortDesc() {
		return shortDesc;
	}

	public void setShortDesc(String shortDesc) {
		this.shortDesc = shortDesc;
	}

	public String getLongDesc() {
		return longDesc;
	}

	public void setLongDesc(String longDesc) {
		this.longDesc = longDesc;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public Product_Type getCategorie() {
		return categorie;
	}

	public void setCategorie(Product_Type categorie) {
		this.categorie = categorie;
	}

}
